package com.wxw.BaseLibrary.component;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.wxw.BaseLibrary.drawable.LayoutParamTool;
import com.wxw.BaseLibrary.tool.ToolsDate;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class WeeksLayout extends LinearLayout implements View.OnClickListener {
    private WeeksLayout.ViewHolder viewHolder = null;
    private String[] weeks = new String[] {"\u5468\u4e00", "\u5468\u4e8c", "\u5468\u4e09", "\u5468\u56db", "\u5468\u4e94", "\u5468\u516d", "\u5468\u65e5"};
    private Handler handler = null;
    private int color = Color.parseColor("#FF5B5B5B");
    private float size = 16.0f;
    private int select_no = 0;
    private int select_yes = 0;
    private int today = 0;
    boolean isDate = false;
    private String date = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE).format(Calendar.getInstance().getTime());

    public WeeksLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    public WeeksLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public WeeksLayout(Context context) {
        super(context);
        initView();
    }

    private void initView() {
        setOrientation(HORIZONTAL);
        setLayoutParams(LayoutParamTool.createParams(10));
        setGravity(Gravity.CENTER);



        viewHolder = new ViewHolder();
        viewHolder.weeks = new TextView[weeks.length];
        viewHolder.ivs = new ImageView[weeks.length-1];
        for (int i=0;i<weeks.length;i++) {
            viewHolder.weeks[i] = createTextView(select_no, weeks[i]);
            viewHolder.weeks[i].setOnClickListener(this);
            viewHolder.weeks[i].setTag(i);
            addView(viewHolder.weeks[i]);
            if (i<weeks.length-1) {
                viewHolder.ivs[i] = new ImageView(getContext());
                viewHolder.ivs[i].setLayoutParams(LayoutParamTool.createParams(0, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                ((LayoutParams) viewHolder.ivs[i].getLayoutParams()).weight = 1;
                addView(viewHolder.ivs[i]);
            }
        }

        if(isDate) {
            setTodayBack();
            return;
        }
    }

    private TextView createTextView(int picID, String text) {
        TextView tv = new TextView(getContext());
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.weight = 1;
        tv.setGravity(Gravity.CENTER);
        tv.setLayoutParams(layoutParams);
        tv.setBackgroundResource(picID);
        tv.setTextColor(Color.parseColor("#ff5b5b5b"));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
        tv.setPadding(3, 3, 3, 3);
        tv.setText(text);
        return tv;
    }

    class ViewHolder {
        private TextView[] weeks;
        private ImageView[] ivs;
    }

    public void setDate() {
        isDate = true;
    }

    public void setBackgroudResource(int select_no, int select_yes, int today, int iv) {
        this.select_no = select_no;
        this.select_yes = select_yes;
        this.today = today;
        for (int i=0;i<viewHolder.ivs.length;i++) {
            viewHolder.ivs[i].setBackgroundResource(iv);
        }
        setAllBackgroud();
    }

    @Override
    public void onClick(View v) {
        if (v instanceof TextView) {
            int index = (Integer)((TextView)v).getTag();
            setAllBackgroud();
            if ((isDate) && (weeks[index].equals(date))) {
                v.setBackgroundResource(today);
            } else {
                v.setBackgroundResource(select_yes);
            }
            if (handler != null) {
                Message msg = new Message();
                msg.what = 99;
                msg.obj = weeks[index];
                msg.arg1 = index;
                handler.sendMessage(msg);
            }
        }
    }

    public void setAllBackgroud() {
        for (int i=0;i<viewHolder.weeks.length;i++) {
            viewHolder.weeks[i].setBackgroundResource(select_no);
            viewHolder.weeks[i].setTextColor(color);
        }
        int week = ToolsDate.getWeekDayIndex();
        if((isDate) && (weeks[week].equals(date))) {
            viewHolder.weeks[week].setBackgroundResource(today);
            viewHolder.weeks[week].setTextColor(Color.parseColor("#FFEEEEEE"));
            return;
        }
        viewHolder.weeks[week].setBackgroundResource(select_no);
    }

    public void setTodayBack() {
        int weekIndex = ToolsDate.getWeekDayIndex();

        if((isDate) && (weeks[weekIndex].equals(date))) {
            viewHolder.weeks[weekIndex].setBackgroundResource(today);
            viewHolder.weeks[weekIndex].setTextColor(Color.parseColor("#FFEEEEEE"));
        } else {
            viewHolder.weeks[weekIndex].setBackgroundResource(select_yes);
        }
        invalidate();
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public void setTextParam(int color, float size) {
        this.color = color;
        this.size = size;
    }
}